package com.heliang.protocol.service;

import java.io.IOException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.heliang.common.FileLog;
import com.heliang.common.StringUtils;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.dataaccess.bean.InfoCatBean;
import com.heliang.dataaccess.bean.MailListBean;
import com.heliang.protocol.baseBean.ReqBodyBaseBean;
import com.heliang.protocol.baseBean.RspBodyBaseBean;
import com.heliang.protocol.requestBean.ReqBodyIndexBean;
import com.heliang.protocol.responseBean.RspBodyIndexBean;
import com.heliang.util.Constants;

/**
 * @ClassName: IndexProtocolImpl.java
 * @Description: 首页数据网络操作
 * @author heliang
 * @version V1.0
 * @Date 2013-10-7 下午4:03:21
 */
public class IndexProtocolImpl extends ProtocolBase {

    /**
     * 网络请求处理
     * @param reqBodyBaseBean
     * @param url //接口地址
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public static RspBodyBaseBean dataProcess(ReqBodyBaseBean reqBodyBaseBean, String url)
        throws JSONException, IOException {

        String rspData = "";
        ReqBodyIndexBean reqBodyIndexBean = (ReqBodyIndexBean) reqBodyBaseBean;
        JSONObject reqJsonObject = new JSONObject();
        reqJsonObject.putOpt("keyvalue", reqBodyIndexBean.getKeyValue());
        reqJsonObject.putOpt("ver_news", reqBodyIndexBean.getVerNews());
        reqJsonObject.putOpt("ver_shops", reqBodyIndexBean.getVerShops());
        reqJsonObject.putOpt("ver_cats", reqBodyIndexBean.getVerInfoCat());
        reqJsonObject.putOpt("site_id", reqBodyIndexBean.getSiteId());
        reqJsonObject.putOpt("edition", reqBodyIndexBean.getEdition());

        String dataStr = reqJsonObject.toString();
        try {
            
            url += URLEncoder.encode(StringUtils.encodeBase64(dataStr.getBytes()), "utf-8");
            rspData = getDataByReqServer(url);
        } catch (Exception e) {
            e.printStackTrace();
            FileLog.log(Constants.TAG + e.getMessage());
            return null;
        }

        /* 解析返回的数据 */
        RspBodyIndexBean repBodyIndexBean = new RspBodyIndexBean();
        JSONObject rspJsonObject = new JSONObject(rspData);

        // 解析资讯版本
        repBodyIndexBean.setVerInfo(rspJsonObject.optInt("ver_news"));

        // 解析资讯类别版本
        repBodyIndexBean.setVerInfoCat(rspJsonObject.optInt("ver_cats"));

        /* 解析推荐资讯信息 */
        String picName = "";
        String imgName = "";
        String currentTimeStr = System.currentTimeMillis() + "";
        String infoPathStr = Constants.SDCARD_PATH + Constants.PATH_INFO;
        String contactPathStr = Constants.SDCARD_PATH + Constants.PATH_TEMP;
        JSONArray infoArray = rspJsonObject.optJSONArray("news");
        if (infoArray != null && infoArray.length() > 0) {
            for (int i = 0; i < infoArray.length(); i++) {
                InfoBean infoBean = new InfoBean();
                infoBean.setCatId((infoArray.getJSONObject(i).getInt("id")));
                infoBean.setType(1); // 代表首页资讯
                infoBean.setCatId(0); // 代表首页推荐资讯
                infoBean.setTitle(infoArray.getJSONObject(i).getString("title"));
                infoBean.setContent(infoArray.getJSONObject(i).getString("desc"));
                infoBean.setCreated(infoArray.getJSONObject(i).getInt("created"));
                infoBean.setUpdateTime(infoArray.getJSONObject(i).getInt("updatetime"));
                infoBean.setCommentCount(infoArray.getJSONObject(i).getInt("comment"));
                infoBean.setFirstNews(true);
                infoBean.setCompanyName(infoArray.getJSONObject(i).getString("companyname"));

                /* 处理资讯缩略 图和大图 */
                JSONArray picsArray = infoArray.getJSONObject(i).optJSONArray("pics");
                if (picsArray != null && picsArray.length() > 0) {
                    picName = "info_" + infoBean.getId() + "_" + currentTimeStr + "_0_1.png";
                    infoBean.setPic1Url(picsArray.getJSONObject(0).optString("pic1"));
                    infoBean.setPic1Path(infoPathStr + picName);

                    picName = "info_" + infoBean.getId() + "_" + currentTimeStr + "_0_2.png";
                    infoBean.setPic2Url(picsArray.getJSONObject(0).optString("pic2"));
                    infoBean.setPic2Path(infoPathStr + picName);
                }

                repBodyIndexBean.getInfoList().add(infoBean);
            }
        }

        /* 解析活跃会员 */
        JSONArray contactArray = rspJsonObject.optJSONArray("mems");
        if (contactArray != null && contactArray.length() > 0) {
            for (int i = 0; i < contactArray.length(); i++) {
                MailListBean contact = new MailListBean();
                contact.setId(contactArray.getJSONObject(i).optInt("id"));
                // Constants.ACTIVEMEMBER.add(contact.getId());
                contact.setUserid(contactArray.getJSONObject(i).optInt("user_id"));
                contact.setUsername(contactArray.getJSONObject(i).optString("user_name"));
                contact.setGender(contactArray.getJSONObject(i).optInt("gender"));
                contact.setPost(contactArray.getJSONObject(i).optString("post"));
                contact.setCompanyname(contactArray.getJSONObject(i).optString("company_name"));
                contact.setTel(contactArray.getJSONObject(i).optString("tel"));
                contact.setMobile(contactArray.getJSONObject(i).optString("mobile"));
                contact.setFax(contactArray.getJSONObject(i).optString("fax"));
                contact.setEmail(contactArray.getJSONObject(i).optString("email"));
                contact.setCatid(contactArray.getJSONObject(i).optInt("cat_id"));
                contact.setCatname(contactArray.getJSONObject(i).optString("cat_name"));
                contact.setProvince(contactArray.getJSONObject(i).optString("province"));
                contact.setCity(contactArray.getJSONObject(i).optString("city"));
                contact.setDistrict(contactArray.getJSONObject(i).optString("district"));
                contact.setAddr(contactArray.getJSONObject(i).optString("addr"));
                contact.setImgurl(contactArray.getJSONObject(i).optString("img"));
                imgName = "contact_" + contact.getId() + "_" + currentTimeStr + ".png";
                if (contact.getImgurl() != null && !contact.getImgurl().equals("")) {
                    contact.setImgpath(contactPathStr + imgName);
                }
                contact.setCreated(contactArray.getJSONObject(i).optInt("created"));
                contact.setUrl(contactArray.getJSONObject(i).optString("url"));

                repBodyIndexBean.getContactList().add(contact);
            }
        }

        /* 解析资讯类别信息 */
        JSONArray infoCatArray = rspJsonObject.optJSONArray("cats");
        if (infoCatArray != null && infoCatArray.length() > 0) {
            for (int i = 0; i < infoCatArray.length(); i++) {
                InfoCatBean infoCatBean = new InfoCatBean();
                infoCatBean.setInfoCatId(infoCatArray.getJSONObject(i).getInt("id"));
                infoCatBean.setInfoCatName(infoCatArray.getJSONObject(i).getString("name"));
                infoCatBean.setInfoCatOrder(infoCatArray.getJSONObject(i).getInt("order"));
                repBodyIndexBean.getInfoCatList().add(infoCatBean);
            }
        }

        /* 解析过期的首页推荐资讯ID */
        JSONArray delsArray = rspJsonObject.optJSONArray("news_dels");
        if (delsArray != null && delsArray.length() > 0) {
            Integer[] ids = new Integer[delsArray.length()];
            for (int i = 0; i < delsArray.length(); i++) {
                ids[i] = delsArray.getJSONObject(i).optInt("id");
            }
            repBodyIndexBean.setInfoDelIds(ids);
            ids = null;
        }

        /* 解析过期的资讯类别ID */
        delsArray = rspJsonObject.optJSONArray("cats_dels");
        if (delsArray != null && delsArray.length() > 0) {
            Integer[] ids = new Integer[delsArray.length()];
            for (int i = 0; i < delsArray.length(); i++) {
                ids[i] = delsArray.getJSONObject(i).optInt("id");
            }
            repBodyIndexBean.setDelCatIds(ids);
            ids = null;
        }
        delsArray = null;

        return repBodyIndexBean;
    }

}
